/*
 * ! Ext JS Library 3.3.0 Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com http://www.extjs.com/license
 */

//
// This is the main layout definition.
//
Ext.chart.Chart.CHART_URL = 'images/charts.swf';
Ext
		.onReady(function() {
			Ext.QuickTips.init();
			// This is an inner body element within the Details panel created to
			// provide
			// a "slide in" effect
			// on the panel body without affecting the body's box itself. This
			// element
			// is created on
			// initial use and cached in this var for subsequent access.

			// This is the main content center region that will contain each
			// example
			// layout panel.
			// It will be implemented as a CardLayout since it will contain
			// multiple
			// panels with
			// only one being visible at any given time.
			var contentPanel = {
				id : 'content-panel',
				region : 'center', // this is what makes this panel into a
									// region
				// within the containing layout
				layout : 'card',
				height : 410,
				margins : '2 5 5 0',
				activeItem : 0,
				border : false,
				autoScroll : true,
				items : [ start, domain, domainList, functionpoint,
						functionList, organization, companyList, role,
						roleList, process, processList, epc, epcList, xmlPanel,
						search
				]
			};
			// 有后台传json对象到建立json数组的方法
			// TreeLoader扩展,支持josn-plugin返回的json对象中包含的数组值
			Ext.tree.JsonPluginTreeLoader = function(config) {
				this.rootName = 'root';
				Ext.tree.JsonPluginTreeLoader.superclass.constructor.call(this,
						config);
			};

			Ext.extend(Ext.tree.JsonPluginTreeLoader, Ext.tree.TreeLoader, {
				processResponse : function(response, node, callback) {
					var json = response.responseText;
					try {
						var o = response.responseData || Ext.decode(json);

						o = eval("(" + o + ")"); // 这个一定要写，不然无法识别成对象
						node.beginUpdate();
						for ( var i = 0, len = o.length; i < len; i++) {
							var n = this.createNode(o[i]);
							if (n) {
								node.appendChild(n);

							}
						}
						node.endUpdate();
						this.runCallback(callback, scope || node, [ node ]);
					} catch (e) {
						this.handleFailure(response);
					}
				}
			});

			// 导航树树建立
			var root = new Ext.tree.AsyncTreeNode(
					{
						id : 'root',
						text : "服务转换功能库",
						expanded : true,
						rootVisible : false,

						children : [
								{
									text : '模型管理',
									id : 'search',
									leaf : true,
									listeners : {
										'click' : function(node, e) {

										}
									}
								},
								{
									text : "模型转换",
									id : 'model',
									children : [
											{
												text : "业务对象模型",
												id : 'domainModelXml',
												loader : new Ext.tree.JsonPluginTreeLoader(
														{
															dataUrl : 'showDomainModelXml'
														})
											},
											{
												text : "EPC模型",
												id : 'epcModelXml',
												loader : new Ext.tree.JsonPluginTreeLoader(
														{
															dataUrl : 'showEpcModelXml'
														}),
												listeners : {
													'click' : function(node, e) {

													}
												}

											},
											{
												text : "WADL模型",
												id : 'wadlModel',
												loader : new Ext.tree.JsonPluginTreeLoader(
														{
															dataUrl : 'showWadlModelXml'
														})

											},
											{
												text : "BPMN模型",
												id : 'bpmnModel',
												loader : new Ext.tree.JsonPluginTreeLoader(
														{
															dataUrl : 'showBpmnModelXml'
														})

											}

									]
								}, 
								{

									text : "视图库",
									children : [ {
										text : "功能视图",
										children : [ {
											id : 'process-list',
											text : '功能结构',
											leaf : true
										/*
										 * loader : new
										 * Ext.tree.JsonPluginTreeLoader( {
										 * dataUrl : 'showProcessTreeNode' })
										 */
										}, {
											id : 'function-list',
											text : '功能库',
											leaf : true
										/*
										 * loader : new
										 * Ext.tree.JsonPluginTreeLoader( {
										 * dataUrl : 'showFunctionTreeNode' })
										 */
										} ]
									}, 
//									{
//										text : "组织视图",
//										children : [
//										// {
//										// id : 'role-list',
//										// text:'角色库',
//										// leaf:true
//										//										
//										// },
										{
											id : 'company-list',
											text : '组织视图',
											leaf : true
									}, {

										text : "业务流程视图",
										id : 'epc-list',
										leaf : true
									/*
									 * loader : new
									 * Ext.tree.JsonPluginTreeLoader({ dataUrl :
									 * 'showEpcModelXml' })
									 */
									}, {

										text : "业务对象视图", // 这里动态导入节点
										id : 'domain-list',
										leaf : true
									/*
									 * loader : new
									 * Ext.tree.JsonPluginTreeLoader({ dataUrl :
									 * 'showDomainTreeNode' })
									 */

									} ]
								}, {
									text : "服务转换",
									id : 'serviceChange',
									children : [ {
										text : "代码生成",
										leaf : true
									}, {
										text : "BPEL",
										leaf : true
									} ]
								}, {
									text : "文件导入",
									children : [ {
										text : "模型导入",
										leaf : true,
										listeners : {
											'click' : function() {
												uploadFileWin.show();
											}
										}
									} ]

								} ]

					});

			// Go ahead and create the TreePanel now so that we can use it below

			var treePanel = new Ext.tree.TreePanel({
				id : 'tree-panel',
				title : '模块选择',
				region : 'center',
				split : true,
				height : 300,
				minSize : 150,
				autoScroll : true,
				root : root

			});

			// alert(getQueryString('type'));
			// Assign the changeLayout function to be called on tree node click.
			treePanel.on('click', function(n) {
				var sn = this.selModel.selNode || {}; // selNode is null on
				// initial
				// selection
				if (n.leaf && n.id != sn.id) { // ignore clicks on folders and
					// currently selected node
					// Ext.getCmp('content-panel').layout.setActiveItem(n.parentNode.id
					// + '-panel');
					Ext.getCmp('content-panel').layout.setActiveItem(n.id
							+ '-panel');
					contentInit(n.parentNode.id, n.id, n.text);

				}
			});
			// Ext.getCmp('domainModelXml').addListener('menu',menufunctino);
			var nodeName;

			treePanel.on('contextmenu', function(node, e) {
				if (node.parentNode.id == 'domainModelXml') {
					if (node.isLeaf()) {
						e.preventDefault;
						node.select();
						nodeName = node.text;
						treeMenu.showAt(e.getXY());

					}
				}
				if (node.id == 'domainModelXml') {
					e.preventDefault;
					node.select();

					refreshMenu.showAt(e.getXY());
				}

				if (node.parentNode.id == 'epcModelXml') {
					if (node.isLeaf()) {
						e.preventDefault;
						node.select();
						nodeName = node.text;
						treeMenu1.showAt(e.getXY());

					}
				}
				if (node.id == 'epcModelXml') {
					e.preventDefault;
					node.select();
					refreshMenu1.showAt(e.getXY());
				}

				if (node.parentNode.id == 'functionModelXml') {
					if (node.isLeaf()) {
						e.preventDefault;
						node.select();
						nodeName = node.text;
						treeMenu2.showAt(e.getXY());

					}
				}

			});

			treeMenu = new Ext.menu.Menu({
				id : 'menufunction',
				items : [ new Ext.menu.Item({

					text : '生成WADL文件',
					icon : 'add',
					handler : clickDelHandler
				})

				]

			});
			treeMenu1 = new Ext.menu.Menu({
				id : 'menufunction1',
				items : [ new Ext.menu.Item({

					text : '生成BPMN文件',
					icon : 'add',
					handler : clickBpmnHandler
				})

				]
			});

			treeMenu2 = new Ext.menu.Menu({
				id : 'menufunction2',
				items : [ new Ext.menu.Item({

					text : '生成WSDL文件',
					icon : 'add',
					handler : clickWsdlHandler
				})

				]
			});
			refreshMenu = new Ext.menu.Menu({
				id : 'refreshmenu',
				items : [ new Ext.menu.Item({
					text : '刷新',
					icon : 'add',
					handler : refresh
				}) ]
			});

			refreshMenu1 = new Ext.menu.Menu({
				id : 'refreshmenu',
				items : [ new Ext.menu.Item({
					text : '刷新',
					icon : 'add',
					handler : refresh1
				}) ]
			});

			var xmldata;
			function clickDelHandler() {
				// Ext.MessageBox.alert(nodeName);
				// Ext.MessageBox.wait('正在处理',nodeName,'等待....'); //this
				// function is useful
				Ext.Ajax.request({
					method : 'POST',// 后台路径
					url : 'generateWadlAction',// 向后台传递参数
					params : {
						nodeName : nodeName
					},
					success : function(request) {
						xmldata = request.responseText;
						xmldata = xmldata.replace("{" + "\"", "");
						xmldata = xmldata.replace("}", "");
						xmldata = xmldata.replace("warnning" + "\"" + ":", "");
						Ext.MessageBox.alert('Generate Wadl', xmldata, null,
								null);
					}

				});
			}
			function clickBpmnHandler() {
				// Ext.MessageBox.alert(nodeName);
				// Ext.MessageBox.wait('正在处理',nodeName,'等待....'); //this
				// function is useful
				Ext.Ajax.request({
					method : 'POST',// 后台路径
					url : 'generateBpmnAction',// 向后台传递参数
					params : {
						nodeName : nodeName
					},
					success : function(request) {
						Ext.MessageBox.alert('Generate Bpmn', 'success', null,
								null);
					}
				});
			}
			function clickWsdlHandler() {
				// Ext.MessageBox.alert(nodeName);
				// Ext.MessageBox.wait('正在处理',nodeName,'等待....'); //this
				// function is useful
				Ext.Ajax.request({
					method : 'POST',// 后台路径
					url : 'generateWsdlAction',// 向后台传递参数
					params : {
						nodeName : nodeName
					},
					success : function(request) {
						xmldata = request.responseText;
						xmldata = xmldata.replace("{" + "\"", "");
						xmldata = xmldata.replace("}", "");
						xmldata = xmldata.replace("warnning" + "\"" + ":", "");
						Ext.MessageBox.alert('Generate Wsdl', xmldata, null,
								null);
					}
				});
			}

			function refresh() {

				root.reload();
				var node = treePanel.getNodeById("model");
				node.expand(); // parent tree should expand first
				var node1 = node.getOwnerTree().getNodeById("domainModelXml");
				node1.expand();
				refreshMenu.hidden;

			}
			function refresh1() {

				root.reload();
				var node = treePanel.getNodeById("model");
				node.expand(); // parent tree should expand first
				var node1 = node.getOwnerTree().getNodeById("epcModelXml");
				node1.expand();
				refreshMenu.hidden;

			}

			// This is the Details panel that contains the description for each
			// example
			// layout.

			// Finally, build the main layout once all the pieces are ready.
			// This is
			// also a good
			// example of putting together a full-screen BorderLayout within a
			// Viewport.
			new Ext.Viewport({
				layout : 'border',
				title : 'WareHouse4WS',
				items : [ {
					xtype : 'box',
					region : 'north',
					applyTo : 'header',
					height : 30
				}, {
					region : 'west', // 左边布局
					title : '导航栏',
					collapsible : true,
					split : true,
					width : 250,
					minSize : 175,
					maxSize : 400,
					layout : 'fit',
					margins : '0 0 0 0',
					items : new Ext.TabPanel({
						border : false,
						activeTab : 0,
						tabPosition : 'bottom',
						items : [ treePanel

						]
					})
				}, contentPanel ],
				renderTo : Ext.getBody()
			});
			// style change
			Ext.get('styleswitcher_select').on('change', function(e, select) {
				var name = select[select.selectedIndex].value;
				setActiveStyleSheet(name);
			});
			var cookie = readCookie("style");
			var title = cookie ? cookie : getPreferredStyleSheet();
			Ext.get('styleswitcher_select').dom.value = title;
		});

// contentPanel填充方法选择
function contentInit(pid, cid, text) {
	if (cid == 'domain-list') {
		showDomainList();
	} else if (cid == 'process-list') {
		showProcessList();
	} else if (cid == 'function-list') {
		showFunctionList();
	} else if (cid == 'company-list') {

		showCompanyList();
	} else if (cid == 'epc-list') {
		showEpcList();
	} else if (cid == 'role-list') {
		showRoleList();
	} else if (pid == 'domainModelXml') {
		showXmlContent(text, "domain");
	} else if (pid == 'wadlModel') {
		showXmlContent(text, "wadl");
	} else if (pid == 'functionModelXml') {
		showXmlContent(text, "function");
	} else if (pid == 'departmentModelXml') {
		showXmlContent(text, "department");
	} else if (pid == 'permissionModelXml') {
		showXmlContent(text, "permission");
	} else if (pid == 'roleModelXml') {
		showXmlContent(text, "role");
	} else if (pid == 'processModelXml') {
		showXmlContent(text, "process");
	} else if (pid == 'epcModelXml') {
		showXmlContent(text, "epc");
	} else if (pid == 'bpmnModel') {
		showXmlContent(text, "bpmn");
	} else if (pid == 'wsdlModel') {
		showXmlContent(text, "wsdl");
	}
}